package com.kaikai.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.kaikai.commonutils.R;
import com.kaikai.servicebase.exceptionhandler.MyException;
import com.kaikai.vod.service.VodService;
import com.kaikai.vod.util.AliyunVodSDKUtils;
import com.kaikai.vod.util.ConstantPropertiesUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Api("阿里云视频点播微服务")
@CrossOrigin //跨域
@RestController
@RequestMapping("eduvod")
public class VodController {
    @Autowired
    VodService vodService;

    //上传视频
    @PostMapping("upload")
    public R uploadVideo(MultipartFile file){
        String result=vodService.uploadVideo(file);
        return R.ok().message("视频上传成功").data("result", result);
    }

    @DeleteMapping("{videoId}")
    public R removeVideo(@PathVariable String videoId) {
        vodService.removeVideoById(videoId);
        return R.ok();
    }

    @DeleteMapping("removeVideos")
    public R removeVideos(@RequestParam("videoList") List<String> videoList) {
        vodService.removeVideoByIds(videoList);
        return R.ok();
    }

    //获取播放凭证
    @GetMapping("getPlayAuth/{vid}")
    public R getPlayAuth(@PathVariable String vid) {
        try {
            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(ConstantPropertiesUtil.ACCESS_KEY_ID, ConstantPropertiesUtil.ACCESS_KEY_SECRET);
            //创建request
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            request.setVideoId(vid);
            //得到response
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            //播放凭证
            String playAuth = response.getPlayAuth();
            return R.ok().data("playAuth", playAuth);
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(20001, "获取凭证失败");
        }
    }


}
